package com.example.braden.answergame.common.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;

import com.example.braden.answergame.R;

/**
 * Created by braden on 2018/4/11.
 */

public class ResultDialog extends Dialog {

    private static Context mContext;
    private static ResultDialog instance;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.iv_img)
    ImageView ivImg;

    public ResultDialog(@NonNull Context context) {
        super(context);
        mContext = context;
        initView();
    }

    public ResultDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
        initView();
    }

    public static ResultDialog getInstance(Context context) {
        if (instance == null || !mContext.equals(context)) {
            synchronized (BackDialog.class) {
                if (instance == null || !mContext.equals(context)) {
                    instance = new ResultDialog(context, R.style.dialog);
                }
            }
        }
        mContext = context;
        return instance;
    }

    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.result_layout, null);
        setContentView(view);
        ButterKnife.bind(this);
        //设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
        getWindow().setBackgroundDrawable(
                new ColorDrawable(mContext.getResources().getColor(R.color.black_50)));
        //一定要在setContentView之后调用，否则无效
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setWindowAnimations(R.style.dialogWindowAnim);
    }

    public void showResult(String title, int imgRes) {
        tvContent.setText(title);
        if (imgRes == 0) {
            ivImg.setVisibility(View.GONE);
        } else {
            ivImg.setImageResource(imgRes);
        }
        show();
    }

    public void closeResult() {
        dismiss();
    }
}
